<template>
  <div>
    <h1>Todo List</h1>
    <div class="main">
      <div id="addTodo">
        <input
          type="text"
          v-model="textObj.text"
          placeholder="请填写内容！"
        /><button @click="addTodo">新建</button>
      </div>
    </div>
    <ul>
      <li v-for="(todo, index) in todos" :key="index" class="todo">
        <p>第{{ index + 1 }}条</p>
        <p v-if="!todo.flag">{{ todo.text }}</p>
        <input type="text" v-model="todo.text" v-else />
        <button class="create" @click="create(index)">删除</button>
        <button @click="edit(index)">
          {{ todos[index].flag ? '保存' : '修改' }}
        </button>
      </li>
    </ul>
    <router-view></router-view>
  </div>
</template>

<script>
export default {
  name: 'commTodolist',
  data: () => ({
    todos: [],
    textObj: {
      text: '',
      flag: false,
    },
  }),
  methods: {
    //tianjia
    addTodo() {
      this.textObj.text = this.textObj.text == '' ? '123' : this.textObj.text
      this.todos.push(this.textObj)
      this.textObj = {
        text: '',
        flag: false,
      }
    },
    //删除
    create(index) {
      this.todos.splice(index, 1)
    },
    //修改
    edit(index) {
      this.todos[index].flag = !this.todos[index].flag
    },
  },
}
</script>

<style  scoped>
* {
  margin: 0;
  padding: 0;
}
.main {
  width: 100%;
  margin-bottom: 5px;
  border: 1px solid black;
}
input {
  margin-right: 10px;
}
#addTodo {
  margin: 10px;
}
ul > li {
  width: 100%;
  list-style: none;
  margin-bottom: 5px;
  border: 1px solid black;
}
.create {
  margin: 5px;
}
</style>